package com.cqndt.site.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class FileUpload {

    public static Result fileUpload(MultipartFile file, String attachmentType,String attachmentUrlSet,String newName,String fileIp) {
        // 获取文件名
        String fileName = file.getOriginalFilename();
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        Boolean flag = isEquals(attachmentType, suffixName);
        if (!flag) {
            return new Result().failure(-1, "请上传正确的类型");
        }
        //保存路径
        String filePath = null;
        if ("2".equals(attachmentType)) {
            filePath = "video"+ File.separator ;
        } else if ("3".equals(attachmentType)) {
            filePath = "file"+ File.separator ;
        } else {
            filePath = "img"+ File.separator ;
        }
        //数据库存放路径
        // 解决文件名重复问题 -- 解决中文问题，liunx下中文路径，图片显示问题
        String attachmentId = UUID.randomUUID().toString();
        String attachmentUrl = filePath + attachmentId + suffixName;
        filePath = filePath + attachmentId;
        File dest = new File(attachmentUrlSet + File.separator + filePath + suffixName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        // 存储文件
        try {
            file.transferTo(dest);
        } catch (Exception e) {
            return new Result().failure(-1, "上传失败,请重新上传");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("attachmentType", attachmentType);
        map.put("imgId", attachmentId);
        map.put("attachmentName", null != newName ? newName : fileName);
        map.put("attachmentUrl", attachmentUrl);
        map.put("viewUrl", fileIp + File.separator + filePath + suffixName);
        return new Result().success(map);
    }

    /**
     * 上传类型控制
     *
     * @param attachmentType 附件类型
     * @param suffixName     附件后缀名
     * @return
     */
    private static Boolean isEquals(String attachmentType, String suffixName) {
        Boolean flag = true;
        if ("1".equals(attachmentType)) {
            flag = suffixName.equalsIgnoreCase(".jpg") || suffixName.equalsIgnoreCase(".png")
                    || suffixName.equalsIgnoreCase(".gif") || suffixName.equalsIgnoreCase(".tif")
                    || suffixName.equalsIgnoreCase(".jpeg") || suffixName.equalsIgnoreCase(".svg")
                    || suffixName.equalsIgnoreCase(".bmp") || suffixName.equalsIgnoreCase(".webp");
        } else if ("3".equals(attachmentType)) {
            flag = suffixName.equalsIgnoreCase(".doc") || suffixName.equalsIgnoreCase(".docx")
                    || suffixName.equalsIgnoreCase(".xls") || suffixName.equalsIgnoreCase(".xlsx")
                    || suffixName.equalsIgnoreCase(".ppt") || suffixName.equalsIgnoreCase(".pptx")
                    || suffixName.equalsIgnoreCase(".pdf") || suffixName.equalsIgnoreCase(".zip")
                    || suffixName.equalsIgnoreCase(".rar") || suffixName.equalsIgnoreCase(".7z")
                    || suffixName.equalsIgnoreCase(".ace") || suffixName.equalsIgnoreCase(".arj")
                    || suffixName.equalsIgnoreCase(".bz2") || suffixName.equalsIgnoreCase(".cab")
                    || suffixName.equalsIgnoreCase(".gz") || suffixName.equalsIgnoreCase(".iso")
                    || suffixName.equalsIgnoreCase(".tar") || suffixName.equalsIgnoreCase(".uue")
                    || suffixName.equalsIgnoreCase(".z") || suffixName.equalsIgnoreCase(".txt");
        } else {
            flag = suffixName.equalsIgnoreCase(".mp4") || suffixName.equalsIgnoreCase(".avi")
                    || suffixName.equalsIgnoreCase(".rmvb") || suffixName.equalsIgnoreCase(".mkv")
                    || suffixName.equalsIgnoreCase(".ogg")
                    || suffixName.equalsIgnoreCase(".flv")
                    || suffixName.equalsIgnoreCase(".wmv");
        }
        return flag;
    }
}
